package com.yixun.qfbao.api.service.impl;

import com.yixun.qfbao.api.service.PremisesImageService;
import com.yixun.qfbao.apidto.SelectPremisesImageListResDto;
import com.yixun.qfbao.mapper.PremisesImageMapper;
import com.yixun.qfbao.model.PremisesImage;
import com.yixun.qfbao.model.dto.PremisesImageDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.xf.model.dto.PremisesPhotoDto;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

@Service("premisesImageService")
public class PremisesImageServiceImpl implements PremisesImageService {
    @Autowired
    private PremisesImageMapper premisesImageMapper;
    @Override
    public int edits(List<PremisesImage> list, Long id) {
        int i = 0;
        for(PremisesImage pi : list){
            pi.setPremisesId(id);
            saveOrEdit(pi);
            i++;
        }
        return i;
    }
    @Override
    public int adds(List<PremisesImage> list,Long premisesId){
        int i = 0;
        for(PremisesImage pi : list){
            pi.setPremisesId(premisesId);
            premisesImageMapper.add(pi);
            i++;
        }
        return i;
    }
    @Override
    public int saveOrEdit(PremisesImage premisesImage){
        if(premisesImage != null && premisesImage.getId() != null){
            premisesImage.setUpdateTime(Calendar.getInstance().getTime());
            return premisesImageMapper.edit(premisesImage);
        }else{
            premisesImage.setCreateTime(Calendar.getInstance().getTime());
            if(DelFlagEnum.DEL.getNo().equals(premisesImage.getIsDeleted())){
                return 0 ;
            }
            premisesImageMapper.add(premisesImage);
            return 1;
        }
    }
	@Override
	public List<PremisesPhotoDto> selectPremisesPhono(Long premisesId) {
		return premisesImageMapper.selectPremisesPhono(premisesId);
	}

    @Override
    public SelectPremisesImageListResDto selectPremisesImageList(Long premisesId) {
        PremisesImageDto premisesImageDto = new PremisesImageDto();
        premisesImageDto.setPremisesId(premisesId);
        List<PremisesImage> list = premisesImageMapper.selectListByDto(premisesImageDto);
        SelectPremisesImageListResDto res = new SelectPremisesImageListResDto();
        res.setResult(ResultEnum.SUCCESS.getNo());
        res.setList(list);
        return res;
    }
}
